﻿using Config.WebApi.Contracts;
using DataServer.ApplicationLib.Services;
using DataServer.CommonLib.Models.DataConfig;
using Microsoft.AspNetCore.Mvc;
using DataServer.CommonLib.Constants;

namespace Config.WebApi.Controllers
{
    [ApiController]
    public class DataConfigController : ControllerBase
    {
        private readonly IDataConfigService _dataConfigService;


        public DataConfigController(IDataConfigService dataConfigService)
        {
            _dataConfigService = dataConfigService;
        }

        // GET: api/<DataConfigController>
        [HttpGet(ApiRoutes.DataConfig.QueryDataConfigs)]
        public async Task<IActionResult> Get([FromQuery] DataConfigSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _dataConfigService.QueryDataConfigsByPageAsync(model);
                return Ok(data);
            }
            else
            {
                var data = await _dataConfigService.QueryDatabasesAsync(model);
                return Ok(data);
            }
        }

        // GET api/<DataConfigController>/5
        [HttpGet(ApiRoutes.DataConfig.GetDataConfig)]
        public async Task<IActionResult> Get(Guid id)
        {
            var data = await _dataConfigService.GetDataConfigAsync(id);
            return Ok(data);
        }

        // POST api/<DataConfigController>
        [HttpPost(ApiRoutes.DataConfig.CreateDataConfig)]
        public async Task<IActionResult> Post([FromBody] DataConfigEditRequestDto model)
        {
            var data = await _dataConfigService.CreateDataConfigAsync(model);
            return Ok(data);
        }

        // PUT api/<DataConfigController>/5
        [HttpPut(ApiRoutes.DataConfig.UpdateDataConfig)]
        public async Task<IActionResult> Put(Guid id, [FromBody] DataConfigEditRequestDto model)
        {
            var data = await _dataConfigService.UpdateDataConfigAsync(id, model);
            return Ok(data);
        }

        // DELETE api/<DataConfigController>/5
        [HttpDelete(ApiRoutes.DataConfig.DeleteDataConfig)]
        public void Delete(int id)
        {
        }

        [HttpGet(ApiRoutes.DataConfig.QueryAvailableColumns)]
        public IActionResult QueryAvailableColumnByDataType(string dataType)
        {
            var data = ConfigColMetaData.GetConfigColMetaData()
                .Where(i => !string.IsNullOrEmpty(i.DataType) && i.DataType.Equals(dataType))
                .ToList();

            return Ok(data);
        }

        [HttpGet(ApiRoutes.DataConfig.QueryFields)]
        public async Task<IActionResult> QueryFieldsByConfig(string configCode,
            [FromQuery] ConfigFieldSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _dataConfigService.QueryFieldsByPageConfigAsync(configCode, model);
                return Ok(data);
            }
            else
            {
                var data = await _dataConfigService.QueryFieldsByConfigAsync(configCode, model);
                return Ok(data);
            }
        }

        [HttpPost(ApiRoutes.DataConfig.CreateField)]
        public async Task<IActionResult> CreateField(string configCode, [FromBody] ConfigFieldEditRequestDto model)
        {
            var data = await _dataConfigService.CreateFieldAsync(configCode, model);
            return Ok(data);
        }

        [HttpGet(ApiRoutes.DataConfig.GetField)]
        public async Task<IActionResult> GetConfigField(Guid id)
        {
            var data = await _dataConfigService.GetConfigFieldAsync(id);
            return Ok(data);
        }

        [HttpPut(ApiRoutes.DataConfig.UpdateField)]
        public async Task<IActionResult> UpdateField(Guid id, [FromBody] ConfigFieldEditRequestDto model)
        {
            var data = await _dataConfigService.UpdateFieldAsync(id, model);
            return Ok(data);
        }


        [HttpGet(ApiRoutes.DataConfig.QueryRules)]
        public async Task<IActionResult> QueryRulesByConfig(string configCode,
            [FromQuery] ConfigRuleSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _dataConfigService.QueryRulesByPageConfigAsync(configCode, model);
                return Ok(data);
            }
            else
            {
                var data = await _dataConfigService.QueryRulesByConfigAsync(configCode, model);
                return Ok(data);
            }
        }

        [HttpPost(ApiRoutes.DataConfig.CreateRule)]
        public async Task<IActionResult> CreateRule(string configCode, [FromBody] ConfigRuleEditRequestDto model)
        {
            var data = await _dataConfigService.CreateRuleAsync(configCode, model);
            return Ok(data);
        }
    }
}